<div class="layuimini-main">
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">借贷金额</label>
            <div class="layui-input-block">
                <input type="number" name="money" lay-verify="required|money" lay-reqtext="金额不能为空" placeholder="请输入投资金额" value=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">借贷时间</label>
            <div class="layui-input-block">
                <input type="text" name="startDate" lay-verify="required|date" lay-reqtext="请选择时间" placeholder="请选择收入时间" value="" id="startTime"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">投资类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="" id="type">
                    <option value="0">借出</option>
                    <option value="1">借入</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label ">备注信息</label>
            <div class="layui-input-block">
                <input type="text" name="remark" lay-verify=""  placeholder="备注不超过100字" value=""
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'axios', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            axios = layui.axios,
            laydate = layui.laydate,
            $ = layui.$;

        //执行一个laydate实例，渲染时间控件
        laydate.render({
            elem: '#startTime' //指定元素
        });

        form.verify({
            money:  function (value, item) {
                if (!new RegExp("^(([1-9][0-9]{0,14})|([0]{1})|(([0]\\.\\d{1,2}|[1-9][0-9]{0,14}\\.\\d{1,2})))$").test(value)){
                    return "请输入正确的金额（大于0）"
                }
            }
        });
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let obj = data.field;

            console.log(obj);
            // 发起异步请求
            axios({
                method: "post",
                url: 'loan/addLoan',
                data: obj
            }).then(response => {
                //打印执行情况
                if (response.code == 0){
                    layer.msg(response.msg, function () {
                        //重载数据表格
                        layui.table.reload('currentTableId');

                        //关闭弹出层
                        layer.close(parentIndex);
                    })
                }else{
                    layer.msg(response.msg);
                }

            }).catch(error => {
                layer.msg(error);
            })

            return false;
        });

    });
</script>